<?php

namespace App\Services\Index;

use App\Services\Service;
use App\Services\Traits\DatabaseMysql;
use App\Models\Game\Settlement\Info as SettlementInfo;
use App\Models\Game\Stats\Online;
use App\Models\Game\Pay\Order\Transfer;
use App\Models\Game\Pay\Order\Wallet;
use App\Core\Traits\Numeral;
use Helper\Timed;
use Ramsey\Uuid\Type\Time;

class ChartService extends Service
{
    use DatabaseMysql, Numeral;

    /**
     * 统计图【在线人数】
     */
    public function online($request)
    {
        // $query = Online::dbTable();
        // // 筛选【平台】
        // $this->makeWhereOrg($request, $query);
        // // 天时间戳
        // $timed = $request->input('datetime', Timed::getDayCurrentTimed());
        // // 查询数据
        // return $query->selectRaw('timed,SUM(online_num) AS online,SUM(gaming_num) AS gaming')
        //     ->where('timed', '>=', $timed)
        //     ->where('timed', '<', Timed::getDayNextTimedTimeZone($timed))
        //     ->groupBy('timed')
        //     ->orderBy('timed')
        //     ->get();

	    $query = Online::dbTable();
	    // 筛选【平台】
	    $this->makeWhereOrg($request, $query);
	    // 天时间戳
	    $timed = $request->input('datetime', Timed::getDayCurrentTimed());
	    $this->makeWhereParamDatetimeTimeZone1($request, $query, 'timed', 'datetime', $timed, $timed);

	    $rows = $query->selectRaw('timed,SUM(online_num) AS online,SUM(gaming_num) AS gaming')
		    ->groupBy('timed')
		    ->orderBy('timed');

//        Timed::toSql($rows);
	    // 查询数据
	    return $rows->get();
    }

    /**
     * 统计图【平台收益】
     */
    public function amount($request)
    {

	    $query = SettlementInfo::dbTable();
	    // 筛选【平台】
	    $this->makeWhereOrg($request, $query);
	    // 筛选【时间】最近100天
	    $this->makeWhereLastDays($request, $query, 'end_time', 100);

	    $collection = $query->selectRaw(implode(',', [
		    "DATE(CONVERT_TZ(DATE_FORMAT(FROM_UNIXTIME(end_time), '%Y-%m-%d %H:%i:%s'), '+08:00', '".Timed::getTimeCode()."')) as timed",
		    "CAST(-SUM(usd->'$.gain_player') AS DECIMAL({$this->decimals()},{$this->precision()})) AS amount",
	    ]))
		    ->where('player_type', SettlementInfo::PLAYER_TYPE)
		    ->groupBy('timed')
		    ->get();
	    // 统计数据
	    //转换指定时区
	    date_default_timezone_set(Timed::getTimezoneName());
	    $collection->transform(function ($item) {
		    $item['timed'] = strtotime($item['timed']);
		    return $item;
	    });
	    return $collection;
    }

    /**
     * 统计图【手工订单】
     */
    public function transfer($request)
    {

	    // 转入
	    $query = Transfer::dbTable();
	    // 筛选【平台】
	    $this->makeWhereOrg($request, $query);
	    // 筛选【时间】最近100天
	    $this->makeWhereLastDays($request, $query, 'created', 100);
	    // 查询数据
	    $in = $query->selectRaw(implode(',', [
		    "DATE(CONVERT_TZ(DATE_FORMAT(FROM_UNIXTIME(created), '%Y-%m-%d %H:%i:%s'), '+08:00', '".Timed::getTimeCode()."')) as timed",
//            "FROM_UNIXTIME(created, '%Y%m%d') AS timed",
		    "CAST(SUM(usd->'$.amount') AS DECIMAL({$this->decimals()},{$this->precision()})) AS amount",
	    ]))
		    ->where('transfer_type', Transfer::TRANSFER_TYPE_IN)
		    ->where('status', Transfer::STATUS_SUCCEED)
		    ->groupBy('timed')
		    ->get();

	    //转换指定时区
	    date_default_timezone_set(Timed::getTimezoneName());
	    $in->transform(function ($item) {
		    $item['timed'] = strtotime($item['timed']);
		    return $item;
	    });

	    // 转出
	    $query = Transfer::dbTable();
	    // 筛选【平台】
	    $this->makeWhereOrg($request, $query);
	    // 筛选【时间】最近100天
	    $this->makeWhereLastDays($request, $query, 'created', 100);
	    // 查询数据
	    $out = $query->selectRaw(implode(',', [
		    "DATE(CONVERT_TZ(DATE_FORMAT(FROM_UNIXTIME(created), '%Y-%m-%d %H:%i:%s'), '+08:00', '".Timed::getTimeCode()."')) as timed",
//            "UNIX_TIMESTAMP(FROM_UNIXTIME(created, '%Y%m%d')) AS timed",
		    "CAST(SUM(usd->'$.amount') AS DECIMAL({$this->decimals()},{$this->precision()})) AS amount",
	    ]))
		    ->where('transfer_type', Transfer::TRANSFER_TYPE_OUT)
		    ->where('status', Transfer::STATUS_SUCCEED)
		    ->groupBy('timed')
		    ->get();
	    $out->transform(function ($item) {
		    $item['timed'] = strtotime($item['timed']);
		    return $item;
	    });

	    return [
		    'in' => $in,
		    'out' => $out
	    ];
    }

    /**
     * 统计图【钱包订单】
     */
    public function wallet($request)
    {

	    // 转入
	    $query = Wallet::dbTable();
	    // 筛选【平台】
	    $this->makeWhereOrg($request, $query);
	    // 筛选【时间】最近100天
	    $this->makeWhereLastDays($request, $query, 'created', 100);
	    // 查询数据
	    $data = $query->selectRaw(implode(',', [
		    "DATE(CONVERT_TZ(DATE_FORMAT(FROM_UNIXTIME(created), '%Y-%m-%d %H:%i:%s'), '+08:00', '".Timed::getTimeCode()."')) as timed",
//            "UNIX_TIMESTAMP(FROM_UNIXTIME(created, '%Y%m%d')) AS timed",
		    "CAST(SUM(usd->'$.consume_sum') AS DECIMAL({$this->decimals()},{$this->precision()})) AS consume_sum",
		    "CAST(SUM(usd->'$.take_back_sum') AS DECIMAL({$this->decimals()},{$this->precision()})) AS take_back_sum",
	    ]))
		    ->where('status', Wallet::STATUS_SUCCEED)
		    ->groupBy('timed')
		    ->get();

	    //转换指定时区
	    date_default_timezone_set(Timed::getTimezoneName());
	    $data->transform(function ($item) {
		    $item['timed'] = strtotime($item['timed']);
		    return $item;
	    });

	    return $data;
    }
}
